package com.gitee.neuray.security.generator.databasemd.biz;

import com.gitee.neuray.security.generator.projectmd.biz.TProjectInfoBiz;
import com.gitee.neuray.security.generator.projectmd.entity.TProjectInfoEntity;
import com.gitee.neuray.security.generator.utils.DataSourceUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.gitee.neuray.security.common.biz.BaseBiz;

import com.gitee.neuray.security.generator.databasemd.mapper.TDatabaseInfoMapper;
import com.gitee.neuray.security.generator.databasemd.entity.TDatabaseInfoEntity;

import javax.sql.DataSource;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TDatabaseInfoBiz extends BaseBiz<TDatabaseInfoMapper,TDatabaseInfoEntity> {
    @Autowired
    private DataSourceUtils dataSource;
    @Autowired
    private TProjectInfoBiz projectInfoBiz;

    @Override
    protected String getPageName() {
        return "TDatabaseInfo"+"Biz";
    }

    /**
     * 测试数据库是否能进行正常访问接口
     * @param tDatabaseInfoEntity
     * @return
     */
    public boolean dbtest(TDatabaseInfoEntity tDatabaseInfoEntity)throws Exception{
        DataSource ds = null;
        if((ds = dataSource.createdb(tDatabaseInfoEntity)) != null){
            return dataSource.excutedb(ds);
        }
        return false;
    }

    @Override
    public void insertSelective(TDatabaseInfoEntity entity) {
        TProjectInfoEntity tProjectInfoEntity = projectInfoBiz.selectById(entity.getProjectId());
        if(tProjectInfoEntity != null){
            entity.setProjectName(tProjectInfoEntity.getProjectName());
        }
        super.insertSelective(entity);
    }
}
